package com.example.demo.Dao;

import com.example.demo.bean.Goods_temp;
import com.example.demo.service.GoodsDao_temp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class Goods_tempImpl implements GoodsDao_temp {
    @Autowired
    private JdbcTemplate jdbcTemplate;


    @Override
    public void add(Goods_temp goods) {
        jdbcTemplate.update("insert into temp_bread(id,score,idclass,orderid,name,price) values(?, ?, ?, ?, ?, ?)",
                goods.getId(),goods.getScore(),goods.getIdclass(),goods.getOrderid(),goods.getName(),goods.getPrice());
    }

    @Override
    public void del(Long orderid) {
        jdbcTemplate.update("delete from temp_bread where orderid=?", orderid);

    }


    @Override
    public int update(Goods_temp goods) {
        return (int)jdbcTemplate.update("update temp_bread set name=?,amount=?,price=? where id=?",
                goods.getName(),goods.getAmount(),goods.getPrice(),goods.getId());
    }

    @Override
    public List<Goods_temp> findGoodsById(Long id,Long orderid) {
        List<Goods_temp> userList = jdbcTemplate.query("select * from temp_bread where orderid=? and id=?",new Object[]{orderid,id},new BeanPropertyRowMapper(Goods_temp.class));
        if(userList!=null && userList.size()>0){
            return  userList;
            //   return  userList.get(0);
        }else{
            return null;
        }
    }

    @Override
    public boolean checkId(Long id,Long orderid) {
        List<Goods_temp> userList = jdbcTemplate.query("select * from temp_bread where orderid=? and id=?",new Object[]{orderid,id},new BeanPropertyRowMapper(Goods_temp.class));
        if(userList==null || userList.size()==0){
            return  false;
            //   return  userList.get(0);
        }else{
            return true;
        }
    }

    @Override
    public List<Goods_temp> findGoodsByName(String name) {
        List<Goods_temp> userList = jdbcTemplate.query("select * from temp_bread where name=?",new Object[]{name},new BeanPropertyRowMapper(Goods_temp.class));
        if(userList!=null && userList.size()>0){
            return  userList;
            //   return  userList.get(0);
        }else{
            return null;
        }
    }


    @Override
    public List<Goods_temp> findGoodsList() {
        List<Goods_temp> userList = jdbcTemplate.query("select * from temp_bread ",new Object[]{},new BeanPropertyRowMapper(Goods_temp.class));
        if(userList!=null && userList.size()>0){
            return  userList;
        }else{
            return null;
        }
    }

    @Override
    public void clear() {
        jdbcTemplate.update("delete from temp_bread ");
    }

    @Override
    public List<Goods_temp> findGoodsByOrderId(Long orderid){
        List<Goods_temp> List = jdbcTemplate.query("select * from temp_bread where orderid=?",new Object[]{orderid},new BeanPropertyRowMapper(Goods_temp.class));
        if(List!=null && List.size()>0){
            return  List;
            //   return  userList.get(0);
        }else{
            return null;
        }
    }
}
